﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Windows;
using System.Diagnostics;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Textbox that displays only the last character and masks all other characters with a password character.
    /// </summary>
    public class PasswordBox : TextBox
    {
        private char passwordChar = '●';
        private UITimer timer;
        private bool showLastChar = false;

        public PasswordBox()
            : base()
        {
            CreateTimer();
        }



        protected override void Dispose(bool dispose)
        {
            DisposeObject(timer);
            timer = null;
            base.Dispose(dispose);
        }

        /// <summary>
        /// Gets or sets the character to be displayed in replace of the real character.
        /// </summary>
        public char PasswordChar
        {
            get { return passwordChar; }
            set
            {
                if (passwordChar != value)
                {
                    passwordChar = value;
                    Invalidate(InvalidationFlag.Appearance);
                }
            }
        }


        private void OnTimerTick(object sender, EventArgs e)
        {
            HideLastChar();
        }

        public override void OnLostFocus()
        {
            HideLastChar();
            if (timer!=null) timer.Stop();
            base.OnLostFocus();
        }

        private void HideLastChar()
        {
            if (showLastChar)
            {
                showLastChar = false;
                stringShape.Text = GetDisplayText();
                Invalidate(InvalidationFlag.Text);
            }
        }


        protected override void OnTextChanged(string oldValue)
        {
            timer.Restart();
            if (!showLastChar)
            {
                showLastChar = true;
            }
            base.OnTextChanged(oldValue);
        }

        protected override string GetDisplayText()
        {
            string text = Text;
            if (text == null) text = string.Empty;
            int l = text.Length;
            StringBuilder sb = new StringBuilder(l);
            char passwordChar = this.passwordChar;
            for (int i = 1; i < l; i++)
            {
                sb.Append(passwordChar);
            }
            if (l > 0) sb.Append(showLastChar ? text[l - 1] : passwordChar);
            return sb.ToString();
        }

        private void CreateTimer()
        {
            timer = new UITimer { Repeated = false, Interval = 2000 };
            timer.Tick += new EventHandler(OnTimerTick);
        }


        protected override void LeftKey()
        {
        }

        protected override void RightKey()
        {
        }

        protected override void HomeKey()
        {
        }

        protected override void EndKey()
        {
        }

        protected internal override void OnBeginPan(PanEventArgs e)
        {
            e.Handled = false;
        }

        protected internal override void OnHold(TouchEventArgs e)
        {
            e.Handled = false;
        }
    }
}
